package cn.niit.pm.service.impl;

import cn.niit.pm.dao.EmployeeMapper;
import cn.niit.pm.entity.Employee;
import cn.niit.pm.service.EmployeeService;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/*
 * fahui
 * 员工业务逻辑层实现类（对需要的数据库操作都通过调用DAO层接口实现）
 * */
@Component
@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Resource
    private EmployeeMapper employeeMapper; //调用员工类DAO层接口

    /**
     * 根据用户名、密码查询用户记录
     * 用于用户登录时校验账号、密码
     *
     * @param employeeName
     * @param password
     * @return
     */
    @Override
    public Employee selectByEmployeeNamePassword(String employeeName, String password) {
        Employee employee = employeeMapper.selectByEmployeeNamePassword(employeeName,password);
        return employee;
    }

    /**
     * 根据查询条件，查询符合所有条件的员工信息
     *
     * @param employee
     * @return
     */
    @Transactional
    @Override
    public List<Employee> selectListByEmployee(Employee employee) {
        /** 开启分页查询，page = 当前第几页， rows = 每页记录数 */
        PageHelper.startPage(employee.getPage(), employee.getRows());
        return employeeMapper.selectListByEmployee(employee);
    }

    /**
     * 添加员工信息
     *
     * @param employee
     * @return
     */
    @Override
    public int insertEmployee(Employee employee) {
        employee.setGmtCreate(new Date());
        int rows = employeeMapper.insertEmployee(employee);
        return rows;
    }

    /**
     * 根据员工ID查询员工信息
     *
     * @param employeeId
     * @return
     */
    @Override
    public  Employee selectByEmployeeId(Long employeeId) {
        return employeeMapper.selectByEmployeeId(employeeId);
    }

    /**
     * 更新员工信息
     *
     * @param employee
     * @return
     */
    @Override
    public int updateEmployee(Employee employee) {
        return employeeMapper.updateEmployee(employee);
    }

    /**
     * 删除员工信息（依据ID）
     *
     * @param employeeId
     * @return
     */
    @Override
    public int deleteByEmployeeId(Long  employeeId) {
        return employeeMapper.deleteByEmployeeId(employeeId);
    }
}
